package ace.tool.generate.code.impl.resolver.scheme.resolver;

import ace.tool.generate.code.impl.resolver.scheme.model.dto.CommonInfo;
import ace.tool.generate.code.impl.service.model.input.GenerateInput;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.apache.commons.text.StringSubstitutor;
import org.apache.commons.text.WordUtils;

/**
 * @author caspar
 * @date 2023/11/17 10:34
 */
@AllArgsConstructor
public class CommonInfoResolver {
  private final GenerateInput input;

  public CommonInfo resolve() {
    return CommonInfo.builder()
        .rootPackageFullName(this.getRootPackageFullName())
        .projectClassName(this.getProjectClassName())
        .build();
  }

  public String getRootPackageFullName() {
    Map<String, String> data = new HashMap<>();
    data.put("codeNamespacePrefix", input.getProjectNamePrefix().replace("-", "."));
    data.put("codeNamespace", input.getProjectName().replace("-", "."));
    return StringSubstitutor.replace("${codeNamespacePrefix}.${codeNamespace}", data);
  }

  public String getProjectClassName() {
    return Arrays.stream(input.getProjectName().split("-"))
        .map(p -> WordUtils.capitalize(p))
        .collect(Collectors.joining(""));
  }
}
